package com.morn.fanfou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.morn.fanfou.api.Fanfou;
import com.morn.fanfou.model.Trends;

public class TrendsListActivity extends ListActivity {
	private Fanfou fanfou;
	private List<Trends> list = null;
	private ProgressBar progressBar;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trends_list);
		fanfou = ((FanfouApplication) this.getApplication()).getFanfou();
		progressBar = (ProgressBar) findViewById(R.id.trendsListProgressBar);
		textView = (TextView) findViewById(R.id.trendsListTextView);
		new TrendsListAsyncTask().execute();

		this.getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String query = list.get(position).getQuery();
				Intent intent = new Intent(TrendsListActivity.this,
						SearchListActivity.class);
				intent.setAction(Intent.ACTION_SEARCH);
				intent.putExtra(SearchManager.QUERY, query);
				TrendsListActivity.this.startActivity(intent);
			}

		});

	}

	private void fillListView() {

		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (Trends trends : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", trends.getName());
			map.put("query", trends.getQuery());
			data.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.trends_item, new String[] { "name", "query" },
				new int[] { R.id.trendsTitleText, R.id.trendsQueryText });
		this.setListAdapter(adapter);

	}

	private class TrendsListAsyncTask extends
			AsyncTask<Object, Object, List<Trends>> {

		@Override
		protected void onPreExecute() {
			progressBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected List<Trends> doInBackground(Object... params) {
			try {
				list = fanfou.getTrendsList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Trends> result) {
			progressBar.setVisibility(View.INVISIBLE);
			if (result != null) {
				fillListView();
			} else {
				textView.setText(R.string.service_no_response);
			}
		}
	}

	public void clickLeftButton(View v) {
		finish();
	}

	public void clickHomeButton(View v) {
		Intent intent = new Intent(this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}
}
